package cn.fch.gotrip.service.impl;

import cn.fch.gotrip.dao.TravelDao;
import cn.fch.gotrip.dao.TravelOrderDao;
import cn.fch.gotrip.dao.UserDao;
import cn.fch.gotrip.entity.Travel;
import cn.fch.gotrip.entity.TravelOrder;
import cn.fch.gotrip.exception.NotFoundException;
import cn.fch.gotrip.exception.ValueException;
import cn.fch.gotrip.service.TravelOrderService;
import cn.fch.gotrip.util.OrderIdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;

/**
 * @program: gotrip
 * @description:
 * @CreatedBy: fch
 * @create: 2023-01-23 17:17
 **/
@Service
@Slf4j
public class TravelOrderServiceImpl implements TravelOrderService {

    @Autowired
    private OrderIdUtil orderIdUtil;

    @Autowired
    private TravelOrderDao travelOrderDao;

    @Autowired
    private TravelDao travelDao;

    @Autowired
    private UserDao userDao;

    @Override
    public boolean creatTravelOrder(TravelOrder travelOrder) {
        if (!userDao.isExistById(travelOrder.getUser().getId())) {
            throw new ValueException("无此用户");
        }
        Travel travel = travelDao.findTravelById(travelOrder.getTravel().getId());
        if (null == travel) {
            throw new ValueException("无此旅行");
        }
        Date now = new Date();
        if (travelOrder.getBeginDate().before(now)) {
            throw new ValueException("日期在今天之前");
        }
//        now = new Date(now.getTime() - 1000 * (new Random().nextInt(60) + 1) * (new Random().nextInt(60) + 1) * (new Random().nextInt(24) + 1) * (new Random().nextInt(30) + 1));
        travelOrder.setEndDate(new Date(travelOrder.getBeginDate().getTime() + 1000 * 60 * 60 * 24 * travel.getDays()));
        travelOrder.setCreatTime(now);
        travelOrder.setTitle(travel.getName());
        travelOrder.setType(new TravelOrder.TravelOrderType(2));
//        travelOrder.setType(new TravelOrder.TravelOrderType(new Random().nextInt(3) + 1));
        travelOrder.setPrice(travel.getPrice());
        travelOrder.setBegin(travel.getBeginning());
        travelOrder.setId(orderIdUtil.creatTravelOrderIdLength21(now));
        log.info("订单{}", travelOrder.toString());
        return travelOrderDao.createTravelOrder(travelOrder) > 0;
    }

    @Override
    public boolean deleteTravelOrder(String id) {
        if (!isNumber(id)){
            throw new ValueException("不合法的id");
        }
        return travelOrderDao.deleteTravelOrder(id) > 0;
    }

    @Override
    public PageInfo<TravelOrder> queryAllTravelOrdersByType(Integer type, Integer pageNum, Integer pageSize) {
        if (type < 1 || type > 3) {
            throw new NotFoundException("错误的type");
        }
        PageHelper.startPage(pageNum, pageSize);
        List<TravelOrder> orderList = travelOrderDao.queryAllTravelOrdersByType(type);
        return new PageInfo<>(orderList);
    }

    @Override
    public PageInfo<TravelOrder> queryByCreatAndType(Integer type, Integer pageNum, Integer pageSize, Long start, Long end) {
        if (start > end){
            throw new ValueException("时间错误");
        }
        Date startTime = new Date(start);
        Date endTime = new Date(end);
        PageHelper.startPage(pageNum, pageSize);
        List<TravelOrder> orderList = travelOrderDao.queryByCreatAndType(type, startTime, endTime);
        return new PageInfo<>(orderList);
    }

    @Override
    public PageInfo<TravelOrder> searchOrderById(String id, Integer pageNum, Integer pageSize) {
        if (!isNumber(id)){
            throw new ValueException("不合法的id");
        }
        PageHelper.startPage(pageNum, pageSize);
        List<TravelOrder> orderList = travelOrderDao.searchOrderById(id);
        return new PageInfo<>(orderList);
    }

    @Override
    public TravelOrder queryOrderById(String id) {
        if (!isNumber(id)){
            throw new ValueException("不合法的id");
        }
        return Optional
                .ofNullable(travelOrderDao.queryOrderById(id))
                .orElseThrow(() -> new NotFoundException("未找到订单"));
    }

    private boolean isNumber(String str){
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }
}
